package com.data.center.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.data.center.dictionary.Availability;
import com.data.center.dto.ExpertDTO;
import com.data.center.dto.PersonDto;
import com.data.center.dto.StaffDto;
import com.data.center.entity.Expert;
import com.data.center.entity.Person;
import com.data.center.entity.Staff;
import com.data.center.exception.ResultBody;
import com.data.center.mapper.ExpertMapper;
import com.data.center.service.ExpertService;
import com.data.center.service.StaffService;
import io.swagger.annotations.Api;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/expert")
@Slf4j
@Api(tags = "专家")
public class ExpertController {

    @Autowired
    ExpertMapper expertMapper;
    @Autowired
    ExpertService expertService;

    @GetMapping("/findExpert")
    public ResultBody findExpert(@RequestBody PersonDto personDto) {
        Person personFilter = new Person();
        org.springframework.beans.BeanUtils.copyProperties(personDto,personFilter);
        return ResultBody.success(expertService.findExpert(personFilter));
    }

    @PostMapping("/addExpert")
    public ResultBody addExpert(@RequestBody PersonDto personDto) {
        Person personFilter = new Person();
        org.springframework.beans.BeanUtils.copyProperties(personDto,personFilter);
        return ResultBody.success(expertService.addExpert(personFilter));
    }

    @PostMapping("/forceAddExpert")
    public ResultBody forceAddExpert(@RequestBody PersonDto personDto) {
        Person personFilter = new Person();
        org.springframework.beans.BeanUtils.copyProperties(personDto,personFilter);
        return ResultBody.success(expertService.forceAddExpert(personFilter));
    }

    // 仅支持可用性更新
    @PostMapping("/updateExpert")
    public ResultBody updateExpert(@RequestBody ExpertDTO expertDTO) {
        Expert expert = new Expert();
        expert.setExpertId(expertDTO.getExpertId());
        expert.setAvailability(expertDTO.getAvailability());
        if (StringUtil.isNotEmpty(expert.getExpertId())
                && StringUtil.isNotEmpty(expert.getAvailability())
                && Availability.contains(expert.getAvailability())) {
            LambdaQueryWrapper<Expert> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Expert::getExpertId,expert.getExpertId());
            return ResultBody.success(expertMapper.update(expert,wrapper));
        }
        return ResultBody.error("输入参数不合规");
    }

    @PostMapping("/logicalDelete")
    public  ResultBody logicalDelete(@RequestBody String id) {
        if (StringUtil.isNotEmpty(id)){
            Expert expert = expertMapper.selectById(id);
            if (expert != null){
                expert.setAvailability(Availability.NOT_EXTRACTABLE.getKey());
                expertMapper.update(expert,new LambdaQueryWrapper<Expert>().eq(Expert::getExpertId,expert.getExpertId()));
                return ResultBody.success();
            } else {
                return ResultBody.error("此id无对应数据");
            }
        } else {
            return ResultBody.error("id为空");
        }
    }

    @DeleteMapping("/physicalDelete")
    public  ResultBody physicalDelete(@RequestBody String id) {
        if (StringUtil.isNotEmpty(id)){
            Expert expert = expertMapper.selectById(id);
            if (expert != null){
                expertMapper.deleteById(id);
                return ResultBody.success();
            } else {
                return ResultBody.error("此id无对应数据");
            }
        } else {
            return ResultBody.error("id为空");
        }
    }
}
